package com.hxzy.controller.front;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.TencentSmsUtil;
import com.hxzy.common.vo.R;
import com.hxzy.dto.RecruitSmsDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 前端短信API
 * @author Administrator
 */
@Api(tags = "前端短信API")
@RestController
@RequestMapping(value = "/front/sms")
public class FrontSmsController extends BaseController {

    @Autowired
    private TencentSmsUtil  tencentSmsUtil;



    @ApiOperation(value = "商家注册发送短信")
    @PostMapping("/send/recruit/{phone}")
    public R sendRecruitSms(@PathVariable(value = "phone") String phone){

        //验证电话号码的有效性
        if( StrUtil.isBlank(phone) ||   !Validator.isMobile(phone)){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

       // 验证手机号发送过短信没有（redis中没有过期的）
        String redisKey=RedisConstans.getRedisKey(RedisConstans.SMS_KEY, SecureUtil.md5(phone));
        if(this.tencentSmsUtil.redisExistsValue(redisKey)){
            R r=R.build(AckCode.SMS_SEND_COLD_TIME_TEMPLATE);
            String msg= StrUtil.format(AckCode.SMS_SEND_COLD_TIME_TEMPLATE.getMsg(), RedisConstans.SMS_EXPIRED_MINUTES);
            r.setMsg(msg);
           return r;
        }
        //测试
        return this.tencentSmsUtil.sendSMSImitate(phone);
        // 发送(上线)
       // return this.tencentSmsUtil.sendSMS(phone);
    }

    @ApiOperation(value = "商家注册短信验证")
    @PostMapping(value = "/recruit/validate")
    public R recruieValidate(@RequestBody @Valid RecruitSmsDTO recruitSmsDTO){
        return this.tencentSmsUtil.recruieValidate(recruitSmsDTO);
    }
}
